package com.huahang.fashionpress.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huahang.fashionpress.enums.ExceptionEnum;
import com.huahang.fashionpress.exception.CartException;
import com.huahang.fashionpress.exception.ResultException;
import com.huahang.fashionpress.mapper.CartMapper;
import com.huahang.fashionpress.mapper.ImgMapper;
import com.huahang.fashionpress.mapper.ProductMapper;
import com.huahang.fashionpress.model.Cart;
import com.huahang.fashionpress.model.Img;
import com.huahang.fashionpress.model.Product;
import com.huahang.fashionpress.service.ICartService;
import com.huahang.fashionpress.vo.ImgVO;
import com.huahang.fashionpress.vo.ProductClientVO;
import com.huahang.fashionpress.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 购物车服务方法
 * @author : 王翰聪
 * @since : 2019/08/25
 * @version : 1.0
 */

@Service
public class CartServiceImpl implements ICartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ImgMapper imgMapper;

    /**
     * 获取购物车商品列表方法
     * @param userID 用户id
     * @param currIndex 当前页号
     * @param pageSize 页面数量
     * @return
     */
    @Override
    public List<ProductClientVO> getCarList(Integer userID, int currIndex, int pageSize) {
        //获取商品列表
        List<Cart> cartList = cartMapper.selectCartListByUserID(userID, currIndex, pageSize);
        List<Product> productList = new ArrayList<Product>();
        for(Cart cart : cartList){
            Product product = productMapper.selectById(cart.getProductId());
            productList.add(product);
        }

        //封装商品列表
        List<ProductClientVO> productClientVOList = new ArrayList<ProductClientVO>();
        for(Product product : productList){
            ProductClientVO productClientVO = new ProductClientVO();
            productClientVO.setProductID(product.getProductId());
            productClientVO.setCategoryId(product.getCategoryId());
            productClientVO.setProductName(product.getProductName());
            productClientVO.setProductPrice(product.getProductPrice());
            //封装购物车中的该商品数量
            productClientVO.setProductQuantity(cartList.get(productList.indexOf(product)).getProductQuantity());
            //封装商品图片
            ImgVO imgVO = new ImgVO();
            //检查商品图片是否存在
            List<Img> imgList = imgMapper.getSingleProductImgList(product.getProductId(), 0);
            if(imgList.isEmpty()){
                imgVO.setImgName("p1.jpg");
            }
            else{
                imgVO.setImgName(imgList.get(0).getImgName());
            }
            productClientVO.setImg(imgVO);
            //添加进列表
            productClientVOList.add(productClientVO);
        }

        return productClientVOList;
    }

    /**
     * 购物车中删除一个商品方法
     * @param productID 商品id
     * @param userID
     * @return
     * @throws ResultException
     */
    @Override
    public boolean deleteSingleProductInCar(String productID, Integer userID) throws ResultException {
        int response = cartMapper.deleteProductByProductID(userID, productID);
        if(response != 1){
            throw new CartException(ExceptionEnum.PRODUCT_IS_NOT_EXSIST);
        }
        return true;
    }

    /**
     * 清空购物车方法
     * @param userID
     * @return
     */
    @Override
    public boolean cartClear(Integer userID) throws CartException {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",1);
        List<Cart> cartList  = cartMapper.selectList(queryWrapper);
        int num = cartList.size();
        if(num <= 0){
            throw new CartException(ExceptionEnum.CART_EMPTY);
        }
        if(num != cartMapper.cartClear(userID)){
            throw new CartException(ExceptionEnum.CART_ERROR);
        }
        return true;
    }

    /**
     * 添加一个商品至购物车方法
     * @param productID
     * @param userID
     * @param cartDate 商品添加日期
     * @param productQuantity 添加的商品数量
     * @return
     */
    @Override
    public boolean addProductIntoCart(String productID, Integer userID, Date cartDate, Integer productQuantity) {
        Date date = new Date();
        //查看商品是否已经存在于购物车中
        Cart cart = cartMapper.selectCartByUserIDAndProductID(userID, productID);
        if(cart != null){
            throw new CartException(ExceptionEnum.PRODUCT_ALREADY_EXSIST);
        }
        //插入商品
        cartMapper.insertProduct(productID, userID, productQuantity, new java.sql.Date(date.getTime()));
        return true;
    }

    /**
     * 修改商品库存
     * @param productID
     * @param userID
     * @param productQuantity 商品数量
     * @return
     */
    @Override
    public boolean updateProductQuantity(String productID, Integer userID,Integer productQuantity) {
        int num = cartMapper.updateProductQuantity(productQuantity, productID, userID);
        if(num == 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public ProductClientVO getProductQuantity(Integer userID, String productID) {
        Cart cart = cartMapper.selectCartByUserIDAndProductID(userID, productID);
        //获取单个商品
        ProductClientVO productVO = new ProductClientVO();
        Product product = productMapper.selectById(productID);
        productVO.setProductID(product.getProductId());
        productVO.setProductPrice(product.getProductPrice());
        productVO.setProductQuantity(cart.getProductQuantity());
        return productVO;
    }
}
